package com.itheima.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 预处理对象PreparedStatement的使用
 *
 * 结论：
 *      只要有可能会传参，就都选用PreparedStatement对象。
 */
public class JdbcDemo3 {

    public static void main(String[] args) throws Exception{
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ee389_jdbc", "root", "1234");
        //3.获取预处理对象
        PreparedStatement pstm = conn.prepareStatement("select * from user where username = ? and password = ?");//使用问号作为参数占位符

        //千万别忘了，给参数占位符赋值
        pstm.setString(1,"柯南");//第一个参数是指定给第几个问号赋值，第二个参数是指定赋值的内容。   第一个参数：从1开始
        pstm.setString(2,"admin");


        //4.执行语句
        ResultSet rs = pstm.executeQuery();
        //5.处理结果
        while(rs.next()){
            int id = rs.getInt("id");
            String username = rs.getString("username");
            String password = rs.getString("password");
            float balance = rs.getFloat("balance");
            System.out.println(id+","+username+","+password+","+balance);
        }
        //6.释放资源
        rs.close();
        pstm.close();
        conn.close();
    }
}
